package flc.ast.activity;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.bean.IdPhotosBean;
import flc.ast.databinding.ActivityCommonPhotoBinding;
import flc.ast.databinding.ActivityCompositionPhotoBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class CompositionPhotoActivity extends BaseAc<ActivityCompositionPhotoBinding> {

    public static Bitmap sBitmap;
    private int type = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_composition_photo;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCompositionPhotoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivCompositionPhotoSave.setOnClickListener(this);
        mDataBinding.rlElectronicSel.setOnClickListener(this);
        mDataBinding.rlTypeSel.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.rlElectronicSel://电子照
                clearSelection();
                type = 0;
                mDataBinding.ivElectronic.setImageResource(R.drawable.dzz);
                mDataBinding.rlElectronic.setVisibility(View.VISIBLE);
                break;
            case R.id.rlTypeSel://排版照
                clearSelection();
                type = 1;
                mDataBinding.ivType.setImageResource(R.drawable.dzz);
                mDataBinding.llType.setVisibility(View.VISIBLE);
                break;
            case R.id.ivCompositionPhotoSave://保存
                saveImg();
                break;
        }
    }

    @Override
    protected void initData() {
        IdPhotosBean bean = Content.sIdPhotosBean;
        mDataBinding.tvPixelW.setText(bean.getPixelW() + getString(R.string.px_text) + " / " + bean.getCyW() + getString(R.string.mm_text));
        mDataBinding.tvPixelH.setText(bean.getPixelH() + getString(R.string.px_text) + " / " + bean.getCyH() + getString(R.string.mm_text));

        mDataBinding.imageViewTouch.setImageBitmap(sBitmap);
        mDataBinding.imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.imageViewTouch.setScaleEnabled(false);// 禁用缩放

        mDataBinding.ivImg1.setImageBitmap(sBitmap);
        mDataBinding.ivImg2.setImageBitmap(sBitmap);
        mDataBinding.ivImg3.setImageBitmap(sBitmap);
        mDataBinding.ivImg4.setImageBitmap(sBitmap);
        mDataBinding.ivImg5.setImageBitmap(sBitmap);
        mDataBinding.ivImg6.setImageBitmap(sBitmap);
        mDataBinding.ivImg7.setImageBitmap(sBitmap);
        mDataBinding.ivImg8.setImageBitmap(sBitmap);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.saving));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap;
                        if (type == 0) {
                            bitmap = ImageUtils.view2Bitmap(mDataBinding.rlSave);
                        } else {
                            bitmap = ImageUtils.view2Bitmap(mDataBinding.llType);
                        }
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_success);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    private void clearSelection() {
        mDataBinding.ivElectronic.setImageResource(R.drawable.pbz);
        mDataBinding.ivType.setImageResource(R.drawable.pbz);
        mDataBinding.rlElectronic.setVisibility(View.GONE);
        mDataBinding.llType.setVisibility(View.GONE);
    }

}